# --------------------------------------------------------
# Fast/er R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Bharath Hariharan
# Rewritten by lucky-ing
# --------------------------------------------------------
import cv2
import re
#
if __name__ =='__main__':
    testfile = "./train1.txt"
    testname =["hat","body"]
    name_id=re.compile('.*/(.*)\.[jpg|jpeg|JPEG|JPG]')
    testfilelist=[]
    with open(testfile, 'r') as f:
        lines = f.readlines()
    for i in lines:
        temp=re.findall(name_id,i)
        testfilelist.append(temp[0])
    for classname in testname:
        detpath = "./{}.txt".format(classname)

        # read dets
        detfile = detpath.format(classname)
        #print(detfile)
        with open(detfile, 'r') as f:
            lines = f.readlines()
            #print(lines)

        # splitlines = [x.strip().split(':') for x in lines]
        splitlines = [x.strip().split(' ') for x in lines]
        for x in splitlines:
            image_id = x[0]
            print(image_id)
            confidence = float(x[1])
            (Xmin,Ymin)=(int(float(x[2])),int(float(x[3])))
            (Xmax,Ymax)=(int(float(x[4])),int(float(x[5])))
            if confidence>0.25:
                fname = "./results/{}.jpg".format(image_id)
                img = cv2.imread(fname)
                # 画矩形框
                cv2.rectangle(img, (Xmin,Ymin), (Xmax,Ymax), (0, 255, 0), 4)
                # 标注文本

                text = classname
                cv2.putText(img, text, (Xmin,Ymin+3),  cv2.FONT_HERSHEY_COMPLEX, 0.75, (0, 0, 255), 1)

                cv2.imwrite(fname, img)
